package com.hcbd.tdwt.ui.fuda.setting.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.hcbd.tdwt.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.pancoit.compression.CompressionInterface;
import com.pancoit.compression.ZDCompression;

import com.hcbd.tdwt.base.AuthManger;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.ui.xingyu.dialog.AuthInfoDialog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_auth_info_fuda)
public class AuthInfoActivity extends BaseActivity {
    @ViewById
    ImageView picInfoImg, voiceInfoImg;
    @ViewById
    LinearLayout picInfoLL, voiceInfoLL;
    @ViewById
    TextView voEffTimeTV, voExpireDateTV, voTotalCallTV, voNumberUsedTV, voRemainUsageTV, voValPeriod;
    @ViewById
    TextView picEffTimeTV, picExpireDateTV, picTotalCallTV, picNumberUsed, picRemainUsage, picValPeriod;
    @ViewById
    Button authVoiceBtn, authPicBtn;
    @ViewById
    TextView voTakeEffectTV, picTakeEffectTV;
    @ViewById
    TextView title;
    private AuthInfoDialog authPicInfoDialog;
    private AuthInfoDialog authVoiceInfoDialog;

    @AfterViews
    protected void init() {
        title.setText("授权信息");
        initDialog();
        initData();

    }



    @Click
    void authVoiceBtn() {
        AuthInfoDialog.show(authVoiceInfoDialog);
    }

    @Click
    void authPicBtn() {
        AuthInfoDialog.show(authPicInfoDialog);
    }

    public void initData() {
        if (AuthManger.getAuthManger().isVoiceOnline()) {
            intOnlineInfo(false);
        } else {
            String str = ZDCompression.getInstance().getVOffInfo();
            intOffInfo(str, false);
        }
        if (AuthManger.getAuthManger().isPicOnline()) {
            intOnlineInfo(true);
        } else {
            String str = ZDCompression.getInstance().getImgOffInfo();
            intOffInfo(str, true);
        }
    }

    @Click
    void voiceInfoAreaRL() {
        if (voiceInfoImg.isSelected()) {
            voiceInfoImg.setSelected(false);
            voiceInfoImg.setBackground(ContextCompat.getDrawable(this, R.drawable.right_arrow_grey_01));
            voiceInfoLL.setVisibility(View.GONE);
        } else {
            voiceInfoLL.setVisibility(View.VISIBLE);
            voiceInfoImg.setSelected(true);
            voiceInfoImg.setBackground(ContextCompat.getDrawable(this, R.drawable.down_arrow_gray_01));
        }

    }

    @Click
    void picInfoAreaRL() {
        if (picInfoImg.isSelected()) {
            picInfoImg.setSelected(false);
            picInfoImg.setBackground(ContextCompat.getDrawable(this, R.drawable.right_arrow_grey_01));
            picInfoLL.setVisibility(View.GONE);
        } else {
            picInfoImg.setSelected(true);
            picInfoLL.setVisibility(View.VISIBLE);
            picInfoImg.setBackground(ContextCompat.getDrawable(this, R.drawable.down_arrow_gray_01));

        }
    }

    void initDialog() {
        authPicInfoDialog = new AuthInfoDialog(this, "图像授权", "图像压缩库激活,请输入授权码。无授权码请联系销售人员", new AuthInfoDialog.OnItemClickListener() {
            @Override
            public void onOK(String str, AuthInfoDialog authInfoDialog) {
                showLoading("加载中");
                ZDCompression.getInstance().initImgZip(AuthInfoActivity.this, str, new CompressionInterface() {
                    @Override
                    public void initCallback(int code) {
                        runOnUiThread(() -> {
                            hideLoading();
                            if (code != 20000) {
                                if (20001 == code || 20100 == code) {
                                    toast("key错误！:" + code);
                                } else if (20010 == code) {
                                    toast("没有网络！:" + code);
                                } else {
                                    toast("其他原因:" + code);
                                }
                            } else {
                                toast("图片压缩库授权成功");
                                AuthManger.getAuthManger().initPicOnline(str);
                                initData();
                            }
                        });

                    }

                    @Override
                    public void zipCallback(int code) {
                        Log.e("zipCallback", ":" + code);
                        if (code != 20000) {
                            toast("图片压缩库压缩失败:" + code);
                        }
                    }

                    ;
                });
                AuthInfoDialog.shutDown(authInfoDialog);
            }

            @Override
            public void onScanning() {
                AuthManger.getAuthManger().initScan(AuthInfoActivity.this, AuthManger.PIC_SC_CODE);
            }
        });
        authVoiceInfoDialog = new AuthInfoDialog(this, "语音授权", "语音压缩库激活,请输入授权码。无授权码请联系销售人员", new AuthInfoDialog.OnItemClickListener() {
            @Override
            public void onOK(String str, AuthInfoDialog authInfoDialog) {
                showLoading("加载中");
                ZDCompression.getInstance().initVoiceZip(AuthInfoActivity.this, str, new CompressionInterface() {
                    @Override
                    public void initCallback(int code) {
                        runOnUiThread(() -> {
                            hideLoading();
                            if (code != 20000) {
                                if (20001 == code || 20100 == code) {
                                    toast("key错误！:" + code);
                                } else if (20010 == code) {
                                    toast("没有网络！:" + code);
                                } else {
                                    toast("其他原因:" + code);
                                }

                            } else {
                                toast("语音压缩库授权成功");
                                AuthManger.getAuthManger().initVoOnline(str);
                                initData();
                            }
                        });
                    }

                    @Override
                    public void zipCallback(int code) {
                        if (code != 20000) {
                            runOnUiThread(() -> toast("语音压缩库压缩失败！:" + code));

                        }
                    }
                });
                AuthInfoDialog.shutDown(authInfoDialog);
            }

            ;

            @Override
            public void onScanning() {
                AuthManger.getAuthManger().initScan(AuthInfoActivity.this, AuthManger.VO_SC_CODE);
            }

            ;
        });
    }

    public void intOnlineInfo(boolean isPic) {
        if (isPic) {
            picEffTimeTV.setText("--");
            picExpireDateTV.setText("--");
            picValPeriod.setText("--");
            picTakeEffectTV.setText("*授权终身有效");
            picTakeEffectTV.setTextColor(getColor(R.color.light_blue));
            picValPeriod.setText("终身有效");
            picTotalCallTV.setText("--");
            picNumberUsed.setText("--");
            picRemainUsage.setText("--");
            authPicBtn.setVisibility(View.GONE);
        } else {
            voEffTimeTV.setText("--");
            voTakeEffectTV.setText("*授权终身有效");
            voTakeEffectTV.setTextColor(getColor(R.color.light_blue));
            voValPeriod.setText("终身有效");
            voExpireDateTV.setText("--");
            voTotalCallTV.setText("--");
            voNumberUsedTV.setText("--");
            voRemainUsageTV.setText("--");
            authVoiceBtn.setVisibility(View.GONE);
        }
    }

    public void intOffInfo(String str, boolean isPic) {
        if ("".equals(str) || str == null) {
            return;
        }
        String[] array = str.split(",");
        int usageCount = Integer.parseInt(array[0]);
        int numberUsed = 100 - usageCount;
        String startDate = array[1];
        String endDate = array[2];
        String currentDate = DateUtils.getDateLongSerial();
        int code = DateUtils.dataContrast(currentDate, endDate);
        if (isPic) {
            picEffTimeTV.setText(startDate);
            picExpireDateTV.setText(endDate);
            picValPeriod.setText(endDate);
            picTotalCallTV.setText("100");
            picNumberUsed.setText(String.valueOf(numberUsed));
            picRemainUsage.setText(String.valueOf(usageCount));
            if (code == -1 || code == 0 || 0 >= usageCount) {
                picTakeEffectTV.setText("*授权已超过有效期");
                picTakeEffectTV.setTextColor(getColor(R.color.colorPrimary));
            } else {
                picTakeEffectTV.setText("*授权已生效");
                picTakeEffectTV.setTextColor(getColor(R.color.light_blue));
            }
        } else {
            voEffTimeTV.setText(startDate);
            voExpireDateTV.setText(endDate);
            voValPeriod.setText(endDate);
            voTotalCallTV.setText("100");
            voNumberUsedTV.setText(String.valueOf(numberUsed));
            voRemainUsageTV.setText(String.valueOf(usageCount));
            if (code == -1 || code == 0 || 0 >= usageCount) {
                voTakeEffectTV.setText("*授权已超过有效期");
                voTakeEffectTV.setTextColor(getColor(R.color.colorPrimary));
            } else {
                voTakeEffectTV.setText("*授权已生效");
                voTakeEffectTV.setTextColor(getColor(R.color.light_blue));
            }

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == AuthManger.PIC_SC_CODE || requestCode == AuthManger.VO_SC_CODE) {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(resultCode, data);
                if (scanResult.getContents() != null) {
                    String result = scanResult.getContents();
                    if (requestCode == AuthManger.PIC_SC_CODE) {
                        authPicInfoDialog.setKeyEt(result);
                    } else {
                        authVoiceInfoDialog.setKeyEt(result);
                    }
                }
            }
        }
    }
    @Click
    void headLeft(){
        finish();
    }
}
